package org.example.dao.impl;

import org.example.dao.AttendanceDao;
import org.example.entity.Attendance;
import org.example.dao.DBUtil; // 需要创建 DBUtil 工具类

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class AttendanceImpl implements AttendanceDao {
    private static String URL = "jdbc:mysql://localhost:3306/db_company?useSSL=false&serverTimezone=UTC";
    private static String USERNAME = "root";
    private static String PASSWORD = "JJH5201314";

    @Override
    public boolean addAttendance(int attId, int empId, java.util.Date date, double workHours, double overHours) {
        String sql = "INSERT INTO attendance (att_id, emp_id, attendance_date, work_hours, over_hours) VALUES (?, ?, ?, ?, ?)";
        try (Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, attId);
            pstmt.setInt(2, empId);
            pstmt.setDate(3, new java.sql.Date(date.getTime()));
            pstmt.setDouble(4, workHours);
            pstmt.setDouble(5, overHours);
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean deleteAttendance(int attendanceId) {
        String sql = "DELETE FROM attendance WHERE att_id = ?";
        try (Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, attendanceId);
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean updateAttendance(int attId, double workHours) {
        String sql = "UPDATE attendance SET work_hours = ? WHERE att_id = ?";
        try (Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setDouble(1, workHours);
            pstmt.setInt(2, attId);
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<Attendance> queryAllAttendance() {
        List<Attendance> result = new ArrayList<>();
        String sql = "SELECT * FROM attendance";
        try (Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            while (rs.next()) {
                Attendance attendance = new Attendance();
                attendance.setAttId(rs.getInt("att_id"));
                attendance.setEmpId(rs.getInt("emp_id"));
                attendance.setAttendanceDate(rs.getDate("attendance_date"));
                attendance.setWorkHours(rs.getDouble("work_hours"));
                attendance.setOverHours(rs.getDouble("over_hours"));
                result.add(attendance);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public List<Attendance> queryAttendanceByEmpId(int empId) {
        List<Attendance> result = new ArrayList<>();
        String sql = "SELECT * FROM attendance WHERE emp_id = ?";
        try (Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, empId);
            try (ResultSet rs = pstmt.executeQuery()) {
                while (rs.next()) {
                    Attendance attendance = new Attendance();
                    attendance.setAttId(rs.getInt("att_id"));
                    attendance.setEmpId(rs.getInt("emp_id"));
                    attendance.setAttendanceDate(rs.getDate("attendance_date"));
                    attendance.setWorkHours(rs.getDouble("work_hours"));
                    attendance.setOverHours(rs.getDouble("over_hours"));
                    result.add(attendance);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }
}